package wang.unclecat.mycard;

import ohos.aafwk.ability.Ability;
import ohos.aafwk.content.Intent;
import ohos.agp.utils.TextTool;
import ohos.event.notification.NotificationRequest;
import ohos.hiviewdfx.HiLog;
import ohos.hiviewdfx.HiLogLabel;
import ohos.rpc.IRemoteObject;
import ohos.rpc.MessageOption;
import ohos.rpc.MessageParcel;
import ohos.rpc.RemoteObject;

public class ServiceAbility extends Ability {
    private static final HiLogLabel LABEL_LOG = new HiLogLabel(HiLog.DEBUG, 0x0, ServiceAbility.class.getSimpleName());
    private static final int NOTICE_ID = 1005;

    private class MyRemoteObject extends RemoteObject {
        private MyRemoteObject() {
            super("MyRemoteObject");
        }

        @Override
        public boolean onRemoteRequest(int code, MessageParcel data, MessageParcel reply, MessageOption option) {
            String str = data.readString();
            HiLog.info(LABEL_LOG, "onRemoteRequest : %{public}d, %{public}s", code, str);
            if (code == 1) {
                notice("ohos : " + str);
            } else if (code == 2) {
                notice("android : " + str);
            }
            return true;
        }
    }

    @Override
    public void onStart(Intent intent) {
        HiLog.error(LABEL_LOG, "ServiceAbility::onStart");
        super.onStart(intent);
        notice("");
    }

    @Override
    public void onBackground() {
        super.onBackground();
        HiLog.info(LABEL_LOG, "ServiceAbility::onBackground");
    }

    @Override
    public void onStop() {
        super.onStop();
        HiLog.info(LABEL_LOG, "ServiceAbility::onStop");
    }

    @Override
    public void onCommand(Intent intent, boolean restart, int startId) {
    }

    @Override
    public IRemoteObject onConnect(Intent intent) {
        return new MyRemoteObject();
    }

    @Override
    public void onDisconnect(Intent intent) {
    }

    private void notice(String text) {
        // 创建通知
        NotificationRequest request = new NotificationRequest(NOTICE_ID);
        NotificationRequest.NotificationNormalContent content = new NotificationRequest.NotificationNormalContent();
        content.setTitle("hello");
        if (!TextTool.isNullOrEmpty(text)) {
            content.setText(text);
        }
        NotificationRequest.NotificationContent notificationContent = new NotificationRequest.NotificationContent(content);
        request.setContent(notificationContent);

        // 绑定通知，1005为创建通知时传入的notificationId
        keepBackgroundRunning(NOTICE_ID, request);
    }
}